﻿/**
 * @des 初始化当前会议室中的成员列表数组，每一个数组元素都为一个对象，
 *      里面存放着成员ID与视频窗口相关的参数。
 */
var winLIndex = 0;		// 左侧列表的开始索引
var winLEndIndex = 0;	// 左侧列表的结束索引
var winRIndex = 0;		// 右侧列表的开始索引

var memberListArray:Array = new Array();

/**
 * @des 显示视频窗口,共显示三行
 * @param place 要显示的位置。接受两种值 left/right
 * @param cp 当前页，如果为空，则认为是第一页
 */
function displayVideoWin(place, cp) {
	var n:Number = 0;		// 显示的个数
	var sum:Number = 3;		// 总的要显示的个数
	var currentX:Number;
	var currentY:Number;
	var startIndex:Number;
	var endIndex:Number;
	
	switch (place.toLowerCase()) {
		case "left":
			currentX = 10;
			currentY = 120;
			startIndex = 0;
			endIndex = winLEndIndex;
			
			// 确定当前的总页
			var countP = endIndex / 3;
			
			if (parseInt(countP) != countP) { countP = parseInt(countP) + 1; }
			var sp = countP.toString();
			
			if (sp == 0) { sp = 1; }
			if (cp == "") {
				lPage.text = "1/" + sp.toString();
			} else {
				startIndex = (cp - 1) * 3;
				endIndex = startIndex + 2;
				
				if (endIndex > winLEndIndex) { endIndex = winLEndIndex; }
				lPage.text = cp.toString() + "/" + sp.toString();
			}
			break;
		case "right":
			currentX = 810;
			currentY = 120;
			startIndex = winRIndex;
			endIndex = memberListArray.length - 1;
			
			if ((endIndex - startIndex) == 0) {
				sp = 1;
			} else {
				var countP = (endIndex - startIndex) / 3;
				
				if (parseInt(countP) != countP) { countP = parseInt(countP) + 1; }
				var sp = countP.toString();
			
				if (sp == 0) { sp = 1; }
			}
			
			if (cp == "") {
				rPage.text = "1/" + sp.toString();
			} else {
				startIndex = (cp - 1) * 3 + winRIndex;
				endIndex = startIndex + 2;
				
				if (endIndex > (memberListArray.length - 1)) { endIndex = memberListArray.length - 1; }
				rPage.text = cp.toString() + "/" + sp.toString();
			}
			break;
		case "center":
			sum = 15;
			currentX = 30;
			currentY = 120;
			startIndex = 0;
			endIndex = memberListArray.length - 1;
			// 确定当前的总页
			var sp = endIndex / (3 * rowN);
			if (parseInt(sp) != sp) { sp = parseInt(sp) + 1; }
			
			if (sp == 0) { sp = 1; }
			if (cp == "") {
				cPage.text = "1/" + sp.toString();
			} else {
				startIndex = (cp - 1) * (3 * rowN);
				endIndex = startIndex + (3 * rowN);
				cPage.text = cp.toString() + "/" + sp.toString();
			}
			break;
		default:
			return false;
	}
	
	var rn:Number = 1;	// 对于讨论会模式，一行要显示rowN个视频
	for (var i = startIndex; i <= endIndex; i++) {
		if (!memberListArray[i].flag && n < sum && memberListArray[i].memberName != undefined) {
			// 创建视频窗口的底窗口
			memberListArray[i].winBase = attachMovie("winBase",
													 "winBase_" + i,
													 this.getNextHighestDepth(),
													 {
														 _x: currentX,
														 _y: currentY
													 });
			memberListArray[i].winBase.index = i;
			memberListArray[i].winBase.x = currentX;
			memberListArray[i].winBase.y = currentY;
			
			var depth = this.getNextHighestDepth();
			// 判断当前对象是否是正在发言
			if (memberListArray[i].isProlocutor) {
				memberListArray[i].winBase.winTxt.htmlText = "<font color='#FF0000'>正在发言</font>";
				memberListArray[i].winObject = currentWin.winObject;
				
				// 調整深度
				memberListArray[i].winObject.swapDepths(depth);
				memberListArray[i].winObject.depth = depth;
				try{
					rs.swapDepths(depth + 1);
					fs.swapDepths(depth + 2);
				} catch(e){}
			} else {
				// 创建视频窗口
				memberListArray[i].winObject = attachMovie("videoWin",
													   "videoWin_" + i,
													   depth,
													   {
															_x: currentX,
															_y: currentY
													   });
			
				// 添加属性
				memberListArray[i].winObject.index = i;
				memberListArray[i].winObject.x = currentX;
				memberListArray[i].winObject.y = currentY;
				memberListArray[i].winObject.depth = depth;
				
				// 加载视频流
				memberListArray[i].winObject.videoWin.attachVideo(memberListArray[i].ns);
				// 播放视频流
				memberListArray[i].ns.play(memberListArray[i].memberId);
				
				if (memberName == currentInfo.data.createUser && currentInfo.data.meetingMode == "大会模式") {
					// 按下鼠标时开启拖动
					memberListArray[i].winObject.onPress = function() {
						this.startDrag();
					
						// 如果是拖动当前发言席上的视频窗口时，则调整视频窗口的大小与坐标值
						if (_xmouse > 200 && _xmouse < 800) {
							this._x = _xmouse - 80;
							this._y = _ymouse - 60;
							this._width = 160;
							this._height = 120;
						}
					};
				
					/**
					 * @des 鼠标释放时停止拖放，如果在发言席上则平铺发言席
					 *      发言席的位置坐标为(200,100)(800,510)
					 */
					memberListArray[i].winObject.onRelease = function() {
						this.stopDrag();
						if (this._x > 200 && this._x < 800 && this._y > 100 && this._y < 510 && memberListArray[this.index].isProlocutor == false) {
							if (!(_xmouse > 300 && _xmouse < 600 && _ymouse > 200 && _ymouse< 410) && memberListArray[this.index].winObject._width == 600) {
								memberListArray[this.index].winObject._x = 200;
								memberListArray[this.index].winObject._y = 100;
								memberListArray[this.index].winObject._width = 600;
								memberListArray[this.index].winObject._height = 410;
								return;
							}
							// 定义alert的事件侦听器
							var listenerObject:Function = function(evt:Object) {
								if (evt.detail == mx.controls.Alert.OK) {
									this.win._x = 200;
									this.win._y = 100;
									this.win._width = 600;
									this.win._height = 410;
									
									memberListArray[this.win.index].winBase.winTxt.htmlText = "<font color='#FF0000'>正在发言</font>";
									
									/**
									 * @des 广播一条设置摄像头采集模式至320*300的命令
									 *      最后一个参数代表模式，true为320*300，false为160*120
									 */
									nc.call("modifyCamera",
											null,
											currentInfo.data.meetingId,
											memberListArray[this.win.index].memberId,
											true);
									// 切换发言人
									nc.call("switchProlocutor",
											null,
											memberListArray[this.win.index].memberId,
											true);
								} else {
									this.win._x = this.win.x;
									this.win._y = this.win.y;
									this.win._width = 160;
									this.win._height = 120;
									
									memberListArray[this.win.index].winBase.winTxt.htmlText = "<font color='#CCCCCC'>视频窗口</font>";
									memberListArray[this.win.index].isProlocutor = false;
									try { _global.tmpSound.setVolume(0); } catch(e) {}
									nc.call("modifyCamera",
											null,
											currentInfo.data.meetingId,
											memberListArray[this.win.index].memberId,
											false);
								}
							};
							
							// 设置alert对话框的按钮的Label值，因为英文版操作系统显示的是ok/cancel
							mx.controls.Alert.okLabel = "确定";
							mx.controls.Alert.cancelLabel = "取消";
							var tmp = mx.controls.Alert.show("是否让" + memberListArray[this.index].memberName + "发言！",
															 "系统提示：",
															 mx.controls.Alert.OK | mx.controls.Alert.CANCEL,
															 _root,
															 listenerObject);
							/**
							 * @des 为mx.controls.Alert.show()的实例tmp添加属性win，指向当前的窗口对象
							 *      主要是用来获取当前窗口的坐标值与大小，因为没有找到如何对侦听对象追加参数的方法．
							 *      在这里有可能不是一个好的方法，虽然能实现，但不知其中是否隐含有其他问题．
							 */
							tmp.win = memberListArray[this.index].winObject;
							tmp.swapDepths(_root.getNextHighestDepth());
						} else {
							if (_xmouse > 200 && _xmouse < 800 && _ymouse > 100 && _ymouse< 510 && memberListArray[this.index].isProlocutor) {
								memberListArray[this.index].winObject._x = 200;
								memberListArray[this.index].winObject._y = 100;
								memberListArray[this.index].winObject._width = 600;
								memberListArray[this.index].winObject._height = 410;
								return;
							}
							if (!(_xmouse > 200 && _xmouse < 800 && _ymouse > 100 && _ymouse< 510)
									&& !memberListArray[this.index].isProlocutor) {
								with (memberListArray[this.index].winObject) {
									_x = x;
									_y = y;
									_width = 160;
									_height = 120;
								}
								return;
							}

							
							
							// 定义alert的事件侦听器
							var listenerObject:Function = function(evt:Object) {
								if (evt.detail == mx.controls.Alert.OK) {
									memberListArray[this.index].winBase.winTxt.htmlText = "<font color='#CCCCCC'>视频窗口</font>";
									this._x = this.x;
									this._y = this.y;
									this._width = 160;
									this._height = 120;
									
									/**
									 * @des 广播一条设置摄像头采集模式至160*120的命令
									 *      最后一个参数代表模式，true为320*300，false为160*120
									 */
									nc.call("modifyCamera",
											null,
											currentInfo.data.meetingId,
											memberListArray[this.win.index].memberId,
											false);
									// 取消发言
									nc.call("halt",
											null,
											memberListArray[this.win.index].memberId);
								} else {
									this.win._x = 200;
									this.win._y = 100;
									this.win._width = 600;
									this.win._height = 410;
								}
							};
							
							// 设置alert对话框的按钮的Label值，因为英文版操作系统显示的是ok/cancel
							mx.controls.Alert.okLabel = "确定";
							mx.controls.Alert.cancelLabel = "取消";
							var tmp = mx.controls.Alert.show("是否消除" + memberListArray[this.index].memberName + "的发言！",
															 "系统提示：",
															 mx.controls.Alert.OK | mx.controls.Alert.CANCEL,
															 _root,
															 listenerObject);
							
							/**
							 * @des 为mx.controls.Alert.show()的实例tmp添加属性win，指向当前的窗口对象
							 *      主要是用来获取当前窗口的坐标值与大小，因为没有找到如何对侦听对象追加参数的方法．
							 *      在这里有可能不是一个好的方法，虽然能实现，但不知其中是否隐含有其他问题．
							 */
							tmp.win = memberListArray[this.index].winObject;
						}
					};
				}
				
			}
			
			// 创建显示标题用的半透明条
			depth = this.getNextHighestDepth() + 1;
			var lblY:Number = currentY + 120 - 20;
			memberListArray[i].winBar = this.attachMovie("videoBar",
														 "videoBar_" + i,
														depth,
														 {
															 _x: currentX,
															 _y: lblY - 3,
															 _alpha: 70
														 });
			memberListArray[i].winBar.index = i;
			memberListArray[i].winBar.x = currentX;
			memberListArray[i].winBar.y = currentY - 3;
			memberListArray[i].alpha = 70;
			memberListArray[i].depth = depth;
			
			// 创建标题
			depth = this.getNextHighestDepth();
			memberListArray[i].winTitle = this.attachMovie("Label",
														   "lbl" + i,
														   depth,
														   {
															   _x: currentX + 2,
															   _y: lblY,
															   _width: 125,
															   html: true,
															   text: memberListArray[i].memberName.split('中心_').join('_')
															});
			
			// 添加属性
			memberListArray[i].winTitle.index = i;
			memberListArray[i].winTitle.x = currentX + 2;
			memberListArray[i].winTitle.y = lblY;
			memberListArray[i].depth = depth;
			
			// 创建视频控制按钮
			depth = this.getNextHighestDepth();
			memberListArray[i].ctrlV = this.attachMovie("playAndPause",
													   "pap_" + i,
													   depth,
													   {
														   _x: currentX + 128,
														   _y: lblY,
														   _alpha: 70
													   });
			
			// 添加属性
			memberListArray[i].ctrlV.x = currentX + 128,
			memberListArray[i].ctrlV.y = lblY;
			memberListArray[i].ctrlV.index = i;
			memberListArray[i].ctrlV.isPlayVideo = true;
			memberListArray[i].depth = depth;
			
			// 视频控制的onPress事件
			memberListArray[i].ctrlV.onPress = function() {
				if (this.isPlayVideo) {
					// 暂停流的播放
					memberListArray[this.index].ns.pause();
					
					this.gotoAndStop(2);
					this.isPlayVideo = false;
				} else {
					// 重新播放流
					memberListArray[this.index].ns.pause();
					
					this.gotoAndStop(1);
					this.isPlayVideo = true;
				}
			};
			
			// 創建承載聲音的mc對象
			depth = this.getNextHighestDepth();
			memberListArray[i].mc = this.createEmptyMovieClip("mc" + i,
															  depth
															  );
			memberListArray[i].mc.attachAudio(memberListArray[i].ns);
			memberListArray[i].mc.depth = depth;
			
			// 創建聲音對象，用來控制聲音
			memberListArray[i].sound = new Sound(memberListArray[i].mc);
			//memberListArray[i].sound.stop();
			
			// 创建声音控制按钮
			depth = this.getNextHighestDepth();
			memberListArray[i].ctrlA = this.attachMovie("sound",
														"s_" + i,
														depth,
														{
															_x: currentX + 143,
															_y: lblY,
															_alpha: 70
														});
			
			// 添加属性
			memberListArray[i].ctrlA.x = currentX + 145;
			memberListArray[i].ctrlA.y = lblY;
			memberListArray[i].ctrlA.index = i;
			memberListArray[i].depth = depth;
			
			// 大會模式下且不是主持人,則不播放聲音.
			if (currentInfo.data.meetingMode == "大会模式" && memberListArray[i].memberName != currentInfo.data.createUser) {
				memberListArray[i].ctrlA.isPlaySound = false;
				//memberListArray[i].sound.stop();
				memberListArray[i].sound.setVolume(0);
				memberListArray[i].ctrlA.gotoAndStop(2);
				trace('不播放聲音');
			} else {
				// 當前成員是主持人那么也不播放聲音
				if (memberName != currentInfo.data.createUser) {
					memberListArray[i].ctrlA.isPlaySound = true;
					//memberListArray[i].sound.start();
					memberListArray[this.index].sound.setVolume(100);
					memberListArray[i].ctrlA.gotoAndStop(1);
					trace('播放聲音');
				} else {
					memberListArray[i].ctrlA.isPlaySound = false;
					//memberListArray[i].sound.stop();
					memberListArray[i].sound.setVolume(0);
					memberListArray[i].ctrlA.gotoAndStop(2);
					trace('不播放聲音');
				}
			}
			
			// 声音控制的onPress事件
			memberListArray[i].ctrlA.onPress = function() {
				if (this.isPlaySound) {
					this.gotoAndStop(2);
					this.isPlaySound = false;
					
					// 停止播放声音
					//memberListArray[this.index].sound.stop();
					memberListArray[this.index].sound.setVolume(0);
				} else {
					this.gotoAndStop(1);
					this.isPlaySound = true;
					
					// 播放声音
					//memberListArray[this.index].sound.start();
					memberListArray[this.index].sound.setVolume(100);
				}
			};
			
			// 设置下一个视频窗口显示的位置
			if (currentInfo.data.meetingMode == "大会模式") {
				currentY = memberListArray[i].winObject.y + 120 + 10;
			} else {
				if (rn % rowN == 0) {
					rn = 0;
					currentX = 30;
					currentY = memberListArray[i].winObject.y + 120 + 10;
				} else {
					currentX = memberListArray[i].winObject.x + 160 + 20;
				}
			}
			
			// 设置当前对象显示的位置
			memberListArray[i].space = n;
			// 设置当前对象已经显示
			memberListArray[i].flag = true;
			
			n++;
			rn++;
		}
	}
}

/**
 * @des 翻页
 * @param p 用来显示页码的TextField对象
 * @pram isNext 是否是下一页，即向上翻页还是向下翻页 p 向上　n 向下
 * @param space 位置，是左侧　还是右侧，left/right
 * @param rn 一行中的个数
 */
function nPage(p, space, isNext, rn) {
	var currentPage = p.text.split("/")[0];
	var pages = p.text.split("/")[1];
	var np;
	
	switch (isNext.toLowerCase()) {
		case "p":
			/**
			 * @des 如果是向上翻页，那么当前页减1，且要大于1
			 */
			if (parseInt(currentPage) > 1) {
				np = parseInt(currentPage) - 1;
			} else {
				return false;
			}
			break;
		case "n":
			/**
			 * @des 如果是向下翻页，那么当前页加1，且小于总页
			 */
			if (parseInt(currentPage) < parseInt(pages)) {
				np = parseInt(currentPage) + 1;
			} else {
				return false;
			}
			break;
		default :
			return false;
	}
	
	/**
	 * @des 清除前一页中的所有视频窗口和与其相关的其他MC
	 *      如果发言人在其被清除的队列之中，则不清除其视频窗口
	 */
	var
		len:Number = currentPage * 3 * rn,
		si:Number = 0;
	
	// 判斷索引值是否已經超過當前頁實際存在的對象索引值
	if (space == "left") {
		if (len > winLEndIndex) { len = winLEndIndex; }
	} else if (space == 'right') {
		si = winRIndex;
	}
	
	for (var i:Number = si + (currentPage - 1) * (3 * rn); i <= si + len; i++) {
		memberListArray[i].flag = false;
		removeMovieClip(memberListArray[i].winBase);
		removeMovieClip(memberListArray[i].winTitle);
		removeMovieClip(memberListArray[i].winBar);
		removeMovieClip(memberListArray[i].ctrlV);
		removeMovieClip(memberListArray[i].ctrlA);
		removeMovieClip(memberListArray[i].mc);
		
		// 若被删除的对象是发言人，则不清除视频窗口，因为此视频窗口正在发言席中．
		if (!memberListArray[i].isProlocutor) {
			memberListArray[i].ns.close();
			removeMovieClip(memberListArray[i].winObject);
		}
	}
	
	/**
	 * @des 显示上/下一页视频窗口
	 *      space表示是左侧(left)还是右侧(right)
	 *      np 要显示的页码
	 */
	displayVideoWin(space, np);
}

/**
 * @des 初始化列表
 */
function initVideoMemberList(str) {
	memberListArray = new Array();
	if (str == "") {
		nc.call("upLog", null, "初始化视频窗口时失败，未获取到当前会议室（" + currentInfo.data.meetingId + "）的成员列表...");
	} else {
		var tmpArray:Array = new Array();
	
		tmpArray = str.split(",");
	
		for (var i:Number = 0; i < tmpArray.length; i++) {
			if (tmpArray[i] != "") {
				var o = new Object();
				o.memberId = getIdOrName(tmpArray[i], "id");		// 视频窗口中的成员ID
				o.memberName = tmpArray[i];							// 视频窗口中的成员名称
				o.winBase;											// 视频窗口的背景窗口
				o.winObject = new Object();							// 视频窗口对象
				o.winTitle;											// 视频窗口的标题
				o.winBar;											// 显示标题用的半透明条
				o.ns = new NetStream(nc);							// 当前的视频流
				o.ctrlV;											// 视频的控制
				o.ctrlA;											// 音頻控制
				o.mc;												// 承載聲音的mc
				o.sound;											// 聲音對象
				o.flag = false;										// 是否已经显示，主要用于分页显示时
				o.isProlocutor = false;								// 是否是发言人
			
				memberListArray.push(o);							// 添加到数组memberListArray中
			}
		}
	}
}

function getLen() {
	// 确定左侧与右侧列表的显示个数
	if (memberListArray.length > 3) {
		winLIndex = 0;
		winLEndIndex = parseInt((memberListArray.length / 2).toString());
		winRIndex = winLEndIndex + 1;
	
		if (memberListArray.length == 6) {
			winLEndIndex = 2;
			winRIndex = 3;
		}
	}
}

function buildList() {
	if (currentInfo.data.meetingMode == "大会模式") {
		displayVideoWin("left", "");
		displayVideoWin("right", "");
	} else {
		displayVideoWin("center", "");
	}
}

initVideoMemberList(currentInfo.data.memberList);
getLen();
buildList();

/**
 * @des 初始化滚动条事件，左右分开控制．
 */
lp.onPress = function() { nPage(lPage, "left", "p", 1); };
ln.onPress = function() { nPage(lPage, "left", "n", 1); };
rp.onPress = function() { nPage(rPage, "right", "p", 1); };
rn.onPress = function() { nPage(rPage, "right", "n", 1); };
cp.onPress = function() { nPage(cPage, "center", "p", rowN); };
cn.onPress = function() { nPage(cPage, "center", "n", rowN); };


// 显示标题
displayTitle();